package com.weitu.risk.controller;

import com.weitu.base.controller.BaseController;
import com.weitu.hiddenDanger.entity.HiddenDangerInfo;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.risk.entity.Region;
import com.weitu.risk.entity.RegionPoint;
import com.weitu.risk.entity.RiskLevel;
import com.weitu.risk.service.RegionPointService;
import com.weitu.risk.service.RegionService;
import com.weitu.util.AjaxResult;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * 风险区域和危险点维护controller
 * Created by fanyuting on 2017/4/28 0028.
 */
@Controller
@RequestMapping("risk/region")
public class RegionController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH+"risk/region/";
    @Autowired
    private RegionService regionService;
    @Autowired
    private RegionPointService pointService;
    /*======================风险区域维护===========================================*/
    /**
     *  跳转到风险区域维护页面
     */
    @RequestMapping("/list")
    public String toRegionPage(){
        return MODULE_BATH_PATH + "list";
    }
    /**
     *  跳转到风险区域简单列表页面
     */
    @RequestMapping("/simpleList")
    public String toRegionSimplePage(){
        return MODULE_BATH_PATH + "simpleList";
    }

    /**
     * 查询风险区域维护列表
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("queryListByParam")
    public Object queryListByParam(HttpServletRequest request, HttpServletResponse response){

        Map<String,Object> map = new HashMap<String,Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = this.regionService.queryListCountByParam(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<Region> list =  this.regionService.queryListByParam(map);
        return  pagination.flushDataGridData(list);
    }

    /**
     * 跳转到风险区域新增页面
     * @return
     */
    @RequestMapping("add")
    public String toRegionAddPage(){

        return MODULE_BATH_PATH + "add";
    }
    /**
     * 跳转到编辑风险区域页面
     * @return
     */
    @RequestMapping("/edit")
    public String toEditRegionPage(HttpServletRequest request, Model model){
        String businessId = request.getParameter("businessId");
        Map<String,Object> map = new HashedMap();
        map.put("id",businessId);
        Region region = this.regionService.queryRegionById(businessId);
        model.addAttribute("region",region);
        return MODULE_BATH_PATH +"edit";
    }
    /**
     * 新增风险区域信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRegionInfo")
    @Transactional
    public AjaxResult addRegionInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean flag = this.regionService.addRegion(map);
        if(flag){
            return success("风险区域信息保存成功！");
        }
        return fail("风险区域信息保存失败！");
    }
    /**
     * 编辑风险区域信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/editRegionInfo")
    @Transactional
    public AjaxResult editRegionInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getEditBasicFieldMap(map);
        boolean flag = this.regionService.editRegion(map);
        if(flag){
            return success("风险区域信息更新成功！");
        }
        return fail("风险区域信息更新失败！");
    }
    /**
     * 删除风险区域信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteRegionInfo")
    @Transactional
    public AjaxResult deleteRegionInfo(HttpServletRequest request, HttpServletResponse response){
        // Map<String,Object> map = new HashMap();
        //getAddBasicFieldMap(map);
        String businessId = request.getParameter("businessId");
        boolean flag = this.regionService.deleteRegionById(businessId);
        if(flag){
            return success("风险区域信息删除成功！");
        }
        return fail("风险区域信息删除失败！");
    }
    @ResponseBody
    @RequestMapping("queryComboboxList")
    @Transactional
    public Object queryComboboxList(HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Map<String,Object>> list = this.regionService.queryComboboxList(map);
        return list;
    }
/*==================风险点维护controller===================*/
    /**
     *  跳转到风险点维护页面
     */
    @RequestMapping("/point/list")
    public String toRegionPointPage(){
        return MODULE_BATH_PATH + "point/list";
    }
    /**
     * 查询风险点维护列表
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("point/queryListByParam")
    public Object queryPointListByParam(HttpServletRequest request, HttpServletResponse response){

        Map<String,Object> map = new HashMap<String,Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = this.pointService.queryListCountByParam(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<RegionPoint> list =  this.pointService.queryListByParam(map);
        return  pagination.flushDataGridData(list);
    }
    @ResponseBody
    @RequestMapping("point/queryComboboxList")
    @Transactional
    public Object queryPointList(HttpServletRequest request,HttpServletResponse response){
        String regionId = request.getParameter("regionId");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("regionId",regionId);
        List<Map<String,Object>> list = this.pointService.queryComboxList(map);
        return list;
    }

    /**
     * 跳转到风险点新增页面
     * @return
     */
    @RequestMapping("point/add")
    public String toRegionPointAddPage(){

        return MODULE_BATH_PATH + "point/add";
    }
    /**
     * 跳转到编辑风险区域页面
     * @return
     */
    @RequestMapping("point/edit")
    public String toEditRegionPointPage(HttpServletRequest request, Model model){
        String businessId = request.getParameter("businessId");
        Map<String,Object> map = new HashedMap();
        map.put("id",businessId);
        RegionPoint point = this.pointService.queryRegionPointById(businessId);
        model.addAttribute("point",point);
        return MODULE_BATH_PATH +"point/edit";
    }
    /**
     * 新增风险点信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRegionPointInfo")
    @Transactional
    public AjaxResult addRegionPointInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean flag = this.pointService.addRegionPoint(map);
        if(flag){
            return success("风险点信息保存成功！");
        }
        return fail("风险点信息保存失败！");
    }
    /**
     * 编辑风险区域信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/editRegionPointInfo")
    @Transactional
    public AjaxResult editRegionPointInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getEditBasicFieldMap(map);
        boolean flag = this.pointService.editRegionPoint(map);
        if(flag){
            return success("风险点信息更新成功！");
        }
        return fail("风险点信息更新失败！");
    }
    /**
     * 删除风险点信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteRegionPointInfo")
    @Transactional
    public AjaxResult deleteRegionPointInfo(HttpServletRequest request, HttpServletResponse response){
        // Map<String,Object> map = new HashMap();
        //getAddBasicFieldMap(map);
        String businessId = request.getParameter("businessId");
        boolean flag = this.pointService.deleteRegionPointById(businessId);
        if(flag){
            return success("风险点信息删除成功！");
        }
        return fail("风险点信息删除失败！");
    }
}
